package view.list_panel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Debate;

public class ListPanel extends JPanel {

	private static final long serialVersionUID = 5371415160036843826L;

	private ParticipantList participants;
	private JButton addButton, removeButton;
	private JPanel buttonPanel;
	
	public ListPanel(final Debate debate) {
		super(new BorderLayout());
		participants = new ParticipantList(debate);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 2));
		
		addButton = new JButton("Add");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog("Please enter the participants name");
				debate.addParticipant(name);
			}});
		removeButton = new JButton("Remove");
		removeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!debate.getParticipants().isEmpty())
					debate.getParticipants().remove(participants.getSelectedIndex());
			}});
		
		buttonPanel.add(addButton);
		buttonPanel.add(removeButton);
		
		add(participants, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);
	}
}
